using UnityEngine;
using System.Collections;

public class Bird : MonoBehaviour {
	
	float speed = 0.4f;
	public GameObject itemTemplate;
	public MoveDirection moveDirection;
	
	void SpawnEgg(){
		//while(true){
		//	yield return new WaitForSeconds(5f);
			if(EggRand()){
				CreateItem(ItemType.egg);
			}
			else
			{
				CreateItem(ItemType.shit);
			}
		//}
	}
	
	bool EggRand(){
		return Random.value>0.3f;
	}
	
	void CreateItem(ItemType type){
		GameObject item = (GameObject)Instantiate(itemTemplate);
		item.GetComponentInChildren<UISprite>().spriteName = type.ToString();
		item.transform.parent = transform;
		item.transform.localPosition = new Vector3(0f, -80f, 0f);
		item.transform.localScale = new Vector3(1f,1f,1f);
		item.transform.parent = GameController.Instance.Panel_PlayingState.transform;
	}
	
	void Start () {
		InvokeRepeating("SpawnEgg",5f,5f);
	}
	
	void FixedUpdate(){
		Vector3 pos = transform.position;
		if(moveDirection==MoveDirection.Right){
			pos.x += speed*Time.deltaTime;
		}
		else{
			pos.x -= speed *Time.deltaTime;
		}
		transform.position = pos;
	}
	
	void ClearScene(){
		GameObject.Destroy(this.gameObject);
	}
		
}

public enum MoveDirection{
	Left=0,
	Right=1
}
